unit UnitClientKommunikation;

interface

//--------------------  ggf Uses-Liste anpassen !  --------------------
uses Classes, mClient;

type
    TClientStatus = (disconnect, login, loginWait, WaitForStart, GameStart);
    TGameStatus = (Offline, Deck, DeckFertig, WaitForPlayer, Player, KarteLeg, KarteGelegt, ChoseAColor, WaitForColor, ColorChosen, EndOfRound);
    TOnlinelistStatus = (Onlinelist, players);
    TDeckStatus = (DeckNachricht, Karten);

  TClientKommunikation = class(TClient)
  private
         Status: TClientStatus;
         GameStatus: TGameStatus;
         OnlinelistStatus: TOnlinelistStatus;
         DeckStatus: TDeckStatus;
         PlayerList, PlayerListTemp: TStringList;
         DeckList, DeckListTemp: TStringList;
         PlayerOnTurn, KarteLeg, NewColor: String;
  public //Methoden
         procedure processMessage(pMessage:string); override;
         constructor create(pServerIP:string; pServerPort:integer);
         function getStatus: TClientStatus;
         procedure setStatus(pStatus: TClientStatus);
         function getGameStatus: TGameStatus;
         procedure setGameStatus(pGameStatus: TGameStatus);
         function getPlayerList: TStringList;
         function getDeckList: TStringList;
         function getPlayerOnTurn:String;
         function getKarteLeg:String;
         function getNewColor:String;
   end;

implementation

//+---------------------------------------------------------------------
//|         TClientKommunikation: Methodendefinition
//+---------------------------------------------------------------------
//Set Status to disconnection -> Wait for connection
constructor TClientKommunikation.create(pServerIP:string; pServerPort:integer);
begin
     inherited;
     Status := disconnect;
     GameStatus := Offline;
     PlayerList := TStringList.create;
     DeckList := TStringList.create;

end;

//process the Message from the server
procedure TClientKommunikation.processMessage(pMessage:string);
var
   i,j: integer;
   test: String;
begin
     case Status of
          //Set Status to login, when answer came
          disconnect:
          begin
               if copy(pMessage,0,11) = '+OK MauMau!' then
                  Status := login;
          end;
          //Wait for Start and Onlinelist, when username is accepted
          loginWait:
          begin
               if copy(pMessage,0,8) = '+OK USER' then
               begin
                  Status := WaitForStart;
                  OnlinelistStatus := Onlinelist;
               end;
          end;
          //Onlinelist or Gamestart!
          WaitForStart:
          begin
                case OnlinelistStatus of

                        //Preperation for Onlinelist Update
                        Onlinelist:
                        begin
                                if copy(pMessage,0,10) = 'ONLINELIST' then
                                begin
                                        OnlinelistStatus := players;
                                        PlayerListTemp := TStringList.create;
                                end;
                        end;

                        //Update Onlinelist
                        players:
                        begin

                                //End of Onlinelist
                                if copy(pMessage,0,1) = '.' then
                                begin
                                        OnlinelistStatus := Onlinelist;
                                        PlayerList := PlayerListTemp;

                                //Add Player to Onlinelist
                                end else if copy(pMessage,0,9) <> '+OK START' then
                                begin
                                        i := Pos(#13 + #10,pMessage)-1;
                                        PlayerListTemp.Add(copy(pMessage,0,i));
                                end;
                        end;
                end;
                //Gamestart!
                if copy(pMessage,0,9) = '+OK START' then
                begin
                        Status := GameStart;
                        GameStatus := Deck;
                        DeckStatus := DeckNachricht;
                end;
          end;
          //Game starts now
          GameStart:
          begin
              case GameStatus of
                //Update for Deck
                Deck:
                begin
                        case DeckStatus of

                                //Preperation for Deckupdate
                                DeckNachricht:
                                begin
                                        if copy(pMessage,0,4) = 'DECK' then
                                        begin
                                                DeckStatus := Karten;
                                                DeckListTemp := TStringList.create;
                                        end;
                                end;

                                //Cardinformation
                                Karten:
                                begin


                                        //End of Cardinformation
                                        if copy(pMessage,0,1) = '.' then
                                        begin
                                                DeckStatus := DeckNachricht;
                                                GameStatus := DeckFertig;
                                                DeckList := DeckListTemp;
                                        end else


                                        //More Cardinfos!
                                        begin
                                               i := Pos(#13 + #10,pMessage)-1;
                                               DeckListTemp.Add(copy(pMessage,0,i));
                                        end;
                                end;
                        end;
                end;
                //Which Player is first?
                WaitForPlayer:
                begin
                        if copy(pMessage,0,6) = 'PLAYER' then
                        begin
                                i := Pos(#13 + #10,pMessage)-8;
                                PlayerOnTurn := copy(pMessage,8,i);


                                GameStatus := Player;
                        end;
                end;
                //This card was chosen!
                KarteGelegt:
                begin
                        if copy(pMessage,0,5) = 'KARTE' then
                        begin
                                i := Pos('FROM',pMessage)-1;
                                KarteLeg := copy(pMessage,7,i-7);
                                i := Pos('INFO',pMessage)+5;
                                if copy(pMessage,i,5) = 'COLOR' then
                                        GameStatus := ChoseAColor
                                else
                                        GameStatus := EndOfRound;
                        end;
                end;
                //Answer from Server
                WaitForColor:
                        if Copy(pMessage,0,5) = 'COLOR' then
                        begin
                                NewColor := Copy(pMessage,7,1);
                                GameStatus := ColorChosen;
                        end;
              end;
          end;
     end;
end;


//Getter and Setter!
function TClientKommunikation.getStatus: TClientStatus;
begin
     result := Status;
end;

procedure TClientKommunikation.setStatus(pStatus: TClientStatus);
begin
     Status := pStatus;
end;

function TClientKommunikation.getGameStatus: TGameStatus;
begin
     result := GameStatus;
end;

procedure TClientKommunikation.setGameStatus(pGameStatus: TGameStatus);
begin
     GameStatus := pGameStatus;
end;

function TClientKommunikation.getPlayerList: TStringList;
begin
        result := PlayerList;
end;

function TClientKommunikation.getDeckList: TStringList;
begin
        result := DeckList;
end;

function TClientKommunikation.getPlayerOnTurn: String;
begin
        result := PlayerOnTurn;
end;

function TClientKommunikation.getKarteLeg:String;
begin
        result := KarteLeg;
end;

function TClientKommunikation.getNewColor:String;
begin
        Result := NewColor;
end;

end.

